{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-12-17T11:58:30.112595Z",
     "start_time": "2024-12-17T11:58:28.938001Z"
    }
   },
   "source": [
    "ZHIPU_API_KEY = '65f85b27091a92ba8fbb32b1bcebfd6a.i6T4GfWNQM5aLF11'\n",
    "\n",
    "# 创建模型实例\n",
    "from langchain_openai import ChatOpenAI  # ChatOpenAI模型\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "    temperature = 0.95,\n",
    "    model_name = \"glm-4-flash\",\n",
    "    openai_api_key = ZHIPU_API_KEY,\n",
    "    openai_api_base = \"https://open.bigmodel.cn/api/paas/v4/\"\n",
    ")"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-17T11:58:53.180790Z",
     "start_time": "2024-12-17T11:58:53.012672Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain import PromptTemplate, LLMChain\n",
    "# 原始字符串模板\n",
    "template = \"{flower}的花语是?\"\n",
    "# 创建LLMChain\n",
    "llm_chain = LLMChain(\n",
    "    llm=llm,\n",
    "    prompt=PromptTemplate.from_template(template))"
   ],
   "id": "da01b0a71a40c3e1",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\zxy\\AppData\\Local\\Temp\\ipykernel_41164\\1657100051.py:5: LangChainDeprecationWarning: The class `LLMChain` was deprecated in LangChain 0.1.17 and will be removed in 1.0. Use :meth:`~RunnableSequence, e.g., `prompt | llm`` instead.\n",
      "  llm_chain = LLMChain(\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-17T11:59:43.735706Z",
     "start_time": "2024-12-17T11:59:40.078504Z"
    }
   },
   "cell_type": "code",
   "source": [
    "result = llm_chain.invoke(\"玫瑰\")\n",
    "print(result)"
   ],
   "id": "206c3f31e5597dd6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'flower': '玫瑰', 'text': '玫瑰花语中，不同颜色和数量的玫瑰有着不同的寓意：\\n\\n1. 红玫瑰：代表热情、热情的爱、热爱。\\n2. 粉玫瑰：代表温柔、感恩、初恋。\\n3. 白玫瑰：代表纯洁、尊敬、我值得你爱。\\n4. 黄玫瑰：代表阳光、快乐、歉意。\\n5. 橙玫瑰：代表热情、激情、敬爱。\\n6. 蓝玫瑰：象征不可能的爱，一种渴望而不可及的情感。\\n7. 紫玫瑰：代表魅力、神秘、梦幻。\\n\\n不同的场合和送给不同的人，选择相应的玫瑰花语来表达情感会更加贴切。'}\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-17T12:01:46.489698Z",
     "start_time": "2024-12-17T12:01:46.486977Z"
    }
   },
   "cell_type": "code",
   "source": [
    "prompt = PromptTemplate(\n",
    "    input_variables=[\"flower\", \"season\"],\n",
    "    template=\"{flower}在{season}的花语是?\",\n",
    ")\n",
    "llm_chain = LLMChain(llm=llm, prompt=prompt)"
   ],
   "id": "55f23d5a0970ef20",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-17T12:02:16.218756Z",
     "start_time": "2024-12-17T12:02:13.309793Z"
    }
   },
   "cell_type": "code",
   "source": "print(llm_chain({'flower': \"玫瑰\", 'season': \"夏季\" }))",
   "id": "8db0e53847b4cb2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'flower': '玫瑰', 'season': '夏季', 'text': '玫瑰在夏季的花语通常是热情、浪漫和温柔的爱情。夏季是一年四季中最充满活力和热情的季节，而玫瑰作为爱情之花，在夏季盛开，象征着在这一年中最为热烈和浓情的爱情。不过，不同颜色和品种的玫瑰可能有不同的花语，例如红玫瑰代表热情如火的爱，粉玫瑰代表温柔的爱和初恋。'}\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-17T12:03:29.707167Z",
     "start_time": "2024-12-17T12:03:27.311415Z"
    }
   },
   "cell_type": "code",
   "source": "llm_chain.run({'flower': \"玫瑰\", 'season': \"夏季\" })",
   "id": "837a09b4629657c2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'玫瑰在夏季的花语通常是“热情如火”或“热情似火”。夏季的玫瑰代表着热烈的情感和对生活的热爱，它传达了夏季热烈、奔放的氛围。此外，不同颜色的玫瑰在夏季也可能有不同的寓意，比如红玫瑰通常代表热情和爱情，而粉色玫瑰则可能代表着温柔和关怀。'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-17T12:26:19.353548Z",
     "start_time": "2024-12-17T12:26:16.746847Z"
    }
   },
   "cell_type": "code",
   "source": "llm_chain.predict(flower=\"玫瑰\", season=\"夏季\")",
   "id": "5fc4388bbf2ef341",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'玫瑰在夏季的花语通常是爱情、热情和美丽。不同的颜色代表不同的含义，如红玫瑰代表热情的爱情，粉玫瑰代表温柔的爱意等。不过，花语也会因地域、文化和习俗的不同而有所变化。夏季的玫瑰特别象征着夏季的繁华与生机，代表着在这个美好的季节里，爱情与美丽更加闪耀。'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-17T14:06:08.658451Z",
     "start_time": "2024-12-17T14:06:02.873611Z"
    }
   },
   "cell_type": "code",
   "source": [
    "input_list = [\n",
    "{\"flower\": \"玫瑰\",'season': \"夏季\"},\n",
    "{\"flower\": \"百合\",'season': \"春季\"},\n",
    "{\"flower\": \"郁金香\",'season': \"秋季\"}\n",
    "]\n",
    "llm_chain.apply(input_list)"
   ],
   "id": "beb3703a9d77c2fb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'text': '玫瑰在夏季的花语通常是“热情”或“爱情”。夏季是玫瑰盛开的季节，人们往往会将玫瑰作为表达爱意和热情的一种方式。不同的颜色和品种的玫瑰，其花语可能有所差异，但普遍来说，夏季的玫瑰都代表着热情和爱情。'},\n",
       " {'text': '百合在春季的花语通常象征着“纯洁、优雅、幸运”。不同的品种可能会有略微不同的寓意，但总体上，百合花都代表着美好的品质和高尚的情感。在古代，百合花还被视为婚姻幸福的象征。'},\n",
       " {'text': '郁金香在秋季的花语通常是“永恒的祝福”或“永恒的美丽”。这是因为郁金香花色繁多，形态优雅，常被用来表达长久不变的美好祝愿和深情厚谊。在不同文化和国家，郁金香的花语可能略有不同，但秋季这一时期，它往往与长久和不变的美好寓意相关联。'}]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-17T14:06:18.708703Z",
     "start_time": "2024-12-17T14:06:12.549574Z"
    }
   },
   "cell_type": "code",
   "source": "llm_chain.generate(input_list)",
   "id": "8231aa7afe1d389f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LLMResult(generations=[[ChatGeneration(text='玫瑰在夏季的花语通常是爱情和热情。夏季是万物生长旺盛的季节，玫瑰在这样的季节里绽放，其花语也就寓意着爱情和热情充满活力，生命力旺盛。不过，不同的颜色玫瑰也有其特定的含义，比如红玫瑰代表热情的爱情，粉色玫瑰代表温柔的爱，白色玫瑰代表纯洁的爱等。', generation_info={'finish_reason': 'stop', 'logprobs': None}, message=AIMessage(content='玫瑰在夏季的花语通常是爱情和热情。夏季是万物生长旺盛的季节，玫瑰在这样的季节里绽放，其花语也就寓意着爱情和热情充满活力，生命力旺盛。不过，不同的颜色玫瑰也有其特定的含义，比如红玫瑰代表热情的爱情，粉色玫瑰代表温柔的爱，白色玫瑰代表纯洁的爱等。', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 70, 'prompt_tokens': 12, 'total_tokens': 82, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'glm-4-flash', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-510d64f8-9412-4321-b88f-66e0b6cf8fed-0', usage_metadata={'input_tokens': 12, 'output_tokens': 70, 'total_tokens': 82, 'input_token_details': {}, 'output_token_details': {}}))], [ChatGeneration(text='百合在春季的花语是“纯洁”。在中国传统文化中，百合花象征着纯洁、高雅和喜庆，常常在婚礼和庆典中使用，寓意美好的生活和高尚的品质。同时，百合还代表着长寿和吉祥，是送给他人的美好祝愿。', generation_info={'finish_reason': 'stop', 'logprobs': None}, message=AIMessage(content='百合在春季的花语是“纯洁”。在中国传统文化中，百合花象征着纯洁、高雅和喜庆，常常在婚礼和庆典中使用，寓意美好的生活和高尚的品质。同时，百合还代表着长寿和吉祥，是送给他人的美好祝愿。', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 54, 'prompt_tokens': 12, 'total_tokens': 66, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'glm-4-flash', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-4f92ece7-cceb-4df5-9e68-29b6ce849e03-0', usage_metadata={'input_tokens': 12, 'output_tokens': 54, 'total_tokens': 66, 'input_token_details': {}, 'output_token_details': {}}))], [ChatGeneration(text='郁金香在秋季的花语通常与爱情和幸运有关。具体来说，秋季的郁金香往往象征着“富有魅力的爱情”和“永恒的祝福”。在某些文化中，它也可能代表了对未来的美好祝愿或者是对亲人朋友的一种祝福。不过，花语的具体含义可能会根据不同地区和传统有所差异。', generation_info={'finish_reason': 'stop', 'logprobs': None}, message=AIMessage(content='郁金香在秋季的花语通常与爱情和幸运有关。具体来说，秋季的郁金香往往象征着“富有魅力的爱情”和“永恒的祝福”。在某些文化中，它也可能代表了对未来的美好祝愿或者是对亲人朋友的一种祝福。不过，花语的具体含义可能会根据不同地区和传统有所差异。', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 71, 'prompt_tokens': 14, 'total_tokens': 85, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'glm-4-flash', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-bd940219-04eb-4395-ad3c-ae0d721f20de-0', usage_metadata={'input_tokens': 14, 'output_tokens': 71, 'total_tokens': 85, 'input_token_details': {}, 'output_token_details': {}}))]], llm_output={'token_usage': {'completion_tokens': 195, 'prompt_tokens': 38, 'total_tokens': 233}, 'model_name': 'glm-4-flash'}, run=[RunInfo(run_id=UUID('510d64f8-9412-4321-b88f-66e0b6cf8fed')), RunInfo(run_id=UUID('4f92ece7-cceb-4df5-9e68-29b6ce849e03')), RunInfo(run_id=UUID('bd940219-04eb-4395-ad3c-ae0d721f20de'))], type='LLMResult')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
